
#include "Animation.h"

Animation::Animation(string filename, int frameTime, int n_sprites)
	: Sprite(filename)
{
	this->frameTime = frameTime;
	this->n_sprites = n_sprites;

	this->clipRect.x = 0;
	this->clipRect.y = 0;
	this->clipRect.h = this->surface->h;
	this->clipRect.w = this->surface->w/n_sprites;

	this->time = ZERO_TIME;
}


void Animation::update(int dt)
{
	time += dt;

	frameSkip = time/frameTime;
	time = time % frameTime;

	clipRect.x += clipRect.w * frameSkip;

	if (clipRect.x >= surface->w)
		clipRect.x = (clipRect.x - surface->w) % surface->w;


	//surface recebeu a sprite original
	//surfaceRotoZoom recepe o clipping da sprite original
	if (surfaceRotoZoom != NULL)
		SDL_FreeSurface(surfaceRotoZoom);

	surfaceRotoZoom = SDLBase::clip(surface, &clipRect);
}



void Animation::setFrame(int frame) {
	clipRect.x = clipRect.w*frame;
	clip(clipRect.x, clipRect.y, clipRect.w, clipRect.h);
}


void Animation::setFrameTime(int time) {
	if (time >= 1) {
		frameTime = time;
	}

}


int Animation::getFrameTime() {
	return frameTime;

}



